//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BatesDoubleExpModel.h"
using namespace Cephei::QL::Models::Equity;
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Equity/HestonModel.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Equity::CBatesDoubleExpModel::CBatesDoubleExpModel (Cephei::QL::Processes::IHestonProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ lambda, Microsoft::FSharp::Core::FSharpOption<Double>^ nuUp, Microsoft::FSharp::Core::FSharpOption<Double>^ nuDown, Microsoft::FSharp::Core::FSharpOption<Double>^ p) : CHestonModel(CBatesDoubleExpModel::typeid)
{
    CHestonProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phBatesDoubleExpModel = NULL;
#endif
        _Cprocess = safe_cast<CHestonProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::HestonProcess>& _process = static_cast<boost::shared_ptr<QuantLib::HestonProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Real _lambda = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (lambda) ? (QuantLib::Real)ValueHelper::Convert (lambda->Value) : 0.1); //4
        QuantLib::Real _nuUp = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (nuUp) ? (QuantLib::Real)ValueHelper::Convert (nuUp->Value) : 0.1); //4
        QuantLib::Real _nuDown = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (nuDown) ? (QuantLib::Real)ValueHelper::Convert (nuDown->Value) : 0.1); //4
        QuantLib::Real _p = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (p) ? (QuantLib::Real)ValueHelper::Convert (p->Value) : 0.5); //4
        _ppBatesDoubleExpModel = new boost::shared_ptr<QuantLib::BatesDoubleExpModel> (new QuantLib::BatesDoubleExpModel ( _process,  _lambda,  _nuUp,  _nuDown,  _p ));
        SetHestonModel (boost::dynamic_pointer_cast<QuantLib::HestonModel> (*_ppBatesDoubleExpModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Models::Equity::CBatesDoubleExpModel::CBatesDoubleExpModel (boost::shared_ptr<QuantLib::BatesDoubleExpModel>& childNative, Object^ owner) : CHestonModel(CBatesDoubleExpModel::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpModel = NULL;
#endif
	_ppBatesDoubleExpModel = &childNative;
    _ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (boost::dynamic_pointer_cast<QuantLib::HestonModel> (*_ppBatesDoubleExpModel));
}
Cephei::QL::Models::Equity::CBatesDoubleExpModel::CBatesDoubleExpModel (QuantLib::BatesDoubleExpModel& childNative, Object^ owner) : CHestonModel(CBatesDoubleExpModel::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpModel = NULL;
#endif
	_ppBatesDoubleExpModel = new boost::shared_ptr<QuantLib::BatesDoubleExpModel> (&childNative);
    _ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (boost::dynamic_pointer_cast<QuantLib::HestonModel> (*_ppBatesDoubleExpModel));
    _BatesDoubleExpModelOwner = owner;
    _HestonModelOwner = owner;
}

Cephei::QL::Models::Equity::CBatesDoubleExpModel::CBatesDoubleExpModel (CBatesDoubleExpModel^ copy) : CHestonModel(CBatesDoubleExpModel::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBatesDoubleExpModel = new boost::shared_ptr<QuantLib::BatesDoubleExpModel> (copy->GetShared());
        _ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (boost::dynamic_pointer_cast<QuantLib::HestonModel> (*_ppBatesDoubleExpModel));
    }
}
Cephei::QL::Models::Equity::CBatesDoubleExpModel::CBatesDoubleExpModel (PLATFORM::Type^ t) : CHestonModel(CBatesDoubleExpModel::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpModel = NULL;
#endif
	if (!t->IsSubclassOf(CBatesDoubleExpModel::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Equity::CBatesDoubleExpModel::CBatesDoubleExpModel (QuantLib::Handle<QuantLib::BatesDoubleExpModel>& childNative, Object^ owner)  : CHestonModel(CBatesDoubleExpModel::typeid)
{
	_phBatesDoubleExpModel = &childNative;
	_ppBatesDoubleExpModel = &static_cast<boost::shared_ptr<QuantLib::BatesDoubleExpModel>>(childNative.currentLink());
    _ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (boost::dynamic_pointer_cast<QuantLib::HestonModel> (*_ppBatesDoubleExpModel));
    _BatesDoubleExpModelOwner = owner;
}
Cephei::QL::Models::Equity::CBatesDoubleExpModel::CBatesDoubleExpModel (QuantLib::Handle<QuantLib::BatesDoubleExpModel> childNative)  : CHestonModel(CBatesDoubleExpModel::typeid)
{
	_phBatesDoubleExpModel = &childNative;
	_ppBatesDoubleExpModel = &static_cast<boost::shared_ptr<QuantLib::BatesDoubleExpModel>>(childNative.currentLink());
    _ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (boost::dynamic_pointer_cast<QuantLib::HestonModel> (*_ppBatesDoubleExpModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Equity::CBatesDoubleExpModel::CBatesDoubleExpModel (QuantLib::BatesDoubleExpModel childNative)  : CHestonModel(CBatesDoubleExpModel::typeid)
{
#ifdef HANDLE
	_phBatesDoubleExpModel = NULL;
#endif
	_ppBatesDoubleExpModel = new boost::shared_ptr<QuantLib::BatesDoubleExpModel> (new QuantLib::BatesDoubleExpModel (childNative));
    _ppHestonModel = new boost::shared_ptr<QuantLib::HestonModel> (boost::dynamic_pointer_cast<QuantLib::HestonModel> (*_ppBatesDoubleExpModel));
}
#endif

Cephei::QL::Models::Equity::CBatesDoubleExpModel::~CBatesDoubleExpModel ()
{
    if (_ppBatesDoubleExpModel != NULL)
    {
	    delete _ppBatesDoubleExpModel;
        _ppBatesDoubleExpModel = NULL;
    }
}
Cephei::QL::Models::Equity::CBatesDoubleExpModel::!CBatesDoubleExpModel ()
{
    if (_ppBatesDoubleExpModel != NULL)
    {
	    delete _ppBatesDoubleExpModel;
    }
}
QuantLib::BatesDoubleExpModel& Cephei::QL::Models::Equity::CBatesDoubleExpModel::GetReference ()
{
    if (_ppBatesDoubleExpModel == NULL) throw REFNEW NativeNullException ();
	return **_ppBatesDoubleExpModel;
}
boost::shared_ptr<QuantLib::BatesDoubleExpModel>& Cephei::QL::Models::Equity::CBatesDoubleExpModel::GetShared ()
{
    if (_ppBatesDoubleExpModel == NULL) throw REFNEW NativeNullException ();
	return *_ppBatesDoubleExpModel;
}
QuantLib::BatesDoubleExpModel* Cephei::QL::Models::Equity::CBatesDoubleExpModel::GetPointer ()
{
    if (_ppBatesDoubleExpModel == NULL) throw REFNEW NativeNullException ();
	return &**_ppBatesDoubleExpModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BatesDoubleExpModel>& Cephei::QL::Models::Equity::CBatesDoubleExpModel::GetHandle ()
{
	if (_phBatesDoubleExpModel == NULL)
	{
		_phBatesDoubleExpModel = new Handle<QuantLib::BatesDoubleExpModel> (*_ppBatesDoubleExpModel);
	}
	return *_phBatesDoubleExpModel;
}
#endif
bool Cephei::QL::Models::Equity::CBatesDoubleExpModel::HasNative () 
{
	return (_ppBatesDoubleExpModel != NULL);
}

Double Cephei::QL::Models::Equity::CBatesDoubleExpModel::Lambda::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBatesDoubleExpModel)->lambda ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CBatesDoubleExpModel::NuDown::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBatesDoubleExpModel)->nuDown ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CBatesDoubleExpModel::NuUp::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBatesDoubleExpModel)->nuUp ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Equity::CBatesDoubleExpModel::P::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBatesDoubleExpModel)->p ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Equity::IBatesDoubleExpModel^ Cephei::QL::Models::Equity::CBatesDoubleExpModel_Factory::Create (Cephei::QL::Processes::IHestonProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ lambda, Microsoft::FSharp::Core::FSharpOption<Double>^ nuUp, Microsoft::FSharp::Core::FSharpOption<Double>^ nuDown, Microsoft::FSharp::Core::FSharpOption<Double>^ p)
{
    return REFNEW CBatesDoubleExpModel ( process,  lambda,  nuUp,  nuDown,  p);
}
